
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Runtime.Remoting;

using Swaf;
using Swaf.Container;
using Swaf.DataAccess;


namespace Swaf.BizRule
{
	/// <summary>
	/// Summary description for BasicRuleContext.
	/// </summary>
	/// <remarks>
	/// This class extends <c>MarshalByRefObject</c> which means that it is used remotely when calling a remote rule manager
	/// with a rule context, or having a rule that is a .NET remoted class.  Currently, not custom proxy is used, so the performance
	/// of accessing the <c>INameAddressSupport</c> members would be bad in a remote call because each <c>get</c> or <c>put</c>
	/// call would be its own network transaction.  A "smart proxy" is intented for this class which knows how to serialize chunks
	/// of the <c>m_contextStorage FlexiMap</c> in order to avoid this type of issue.  For now, stay clear of doing remote rule calls
	/// and passing context for this reason (4/29/04).
	/// </remarks>
	public class BasicRuleContext : MarshalByRefObject, IRuleContext, INameAddressSupport
	{
		protected IApplication m_app;
		protected IRuleContext m_parent;
		protected FlexiMap m_contextStorage = new FlexiMap();

		public BasicRuleContext(IApplication app)
		{
			m_app = app;
		}

		public BasicRuleContext(IApplication app, IRuleContext parent)
			:this(app)
		{
			m_parent = parent;
		}

		public virtual IRuleGroup RuleGroup {get{return null;}}

		public virtual IApplication App {get{return m_app;}}

		public virtual IRuleContext ParentContext {get{return m_parent;}}

		public virtual IRuleContext createSubContext() 
		{
			return new BasicRuleContext(m_app, this);
		}

		public ITransaction CurrentTransaction
		{
			get
			{
				IRunningRule rule = ((IRuleManagerEx)Application.currentApp.ruleMgr).CurrentThreadRunningRule;
				ITransaction t = (rule==null) ? null : (ITransaction)rule["_transaction:" + rule.UniqueId.ToString()];
				return t;
			}
		}

		public void voteCommit()
		{
			ITransaction t = CurrentTransaction;
			if(t != null) t.voteCommit();
		}

		public void voteRollback()
		{
			ITransaction t = CurrentTransaction;
			if(t != null) t.voteRollBack();
		}


		#region INameAddressSupport Members

		public bool isGetOnly {get{return false;}}

		public Swaf.Container.XmlSerializationSupport SerializationSupport
		{
			get
			{
				// TODO:  Add BasicRuleContext.SerializationSupport getter implementation
				return new Swaf.Container.XmlSerializationSupport ();
			}
		}

		public object get(string nameAddress, object oneArg)
		{
			return this.get(nameAddress, new object[]{oneArg});
		}

		public object get(string nameAddress)
		{
			return this.get(nameAddress, new object[]{});
		}

		public object get(string nameAddress, params object[] infoArgs)
		{
			string firstName = NameAddress.getFirstAddress(nameAddress);
			if(m_contextStorage.containsField(firstName))
				return m_contextStorage.get(nameAddress, infoArgs);
			else
			{
				if (m_parent != null)
					return m_parent.get(nameAddress, infoArgs);
				else
					return null;
			}
		}

		public bool containsField(string name)
		{
			if(m_contextStorage.containsField(name))
				return true;
			else
				return parentContainsField(name);
		}

		public void put(string nameAddress, object val, object extraInfo)
		{
			if(parentContainsField(NameAddress.getFirstAddress(nameAddress)))
				m_parent.put(nameAddress, val, extraInfo);
			else
			{
				if(extraInfo != null && NameAddress.isSingleAddress(nameAddress) && 
					extraInfo is String && String.Compare((string)extraInfo, "remove", true) == 0)
					m_contextStorage.remove(nameAddress);
				else
					m_contextStorage.put(nameAddress, val, extraInfo);
			}
		}

		public void put(string nameAddress, object val)
		{
			if(parentContainsField(NameAddress.getFirstAddress(nameAddress)))
				m_parent.put(nameAddress, val);
			else
				m_contextStorage.put(nameAddress, val);
		}

		public string getXml(string options)
		{
			return Xml;
		}

		public string Xml
		{
			get
			{
				return "<RuleContext>Not Implemented Yet. TBD.</RuleContext>";
			}
		}
		private bool parentContainsField(string nameAddress)
		{
			if (m_parent == null)
				return false;
			return m_parent.containsField(nameAddress);				
		}
		#endregion
		public IEnumerator GetEnumerator() { return null; }
	}
}
